package com.jinke.uang.ui.activity.personal;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jinke.uang.R;
import com.jinke.uang.base.BaseNormalActivity;
import com.jinke.uang.base.BaseResponse;
import com.jinke.uang.bean.LoginData;
import com.jinke.uang.cache.Acache;
import com.jinke.uang.common.RouterMap;
import com.jinke.uang.common.UserInfo;
import com.jinke.uang.constant.Constant;
import com.jinke.uang.constant.HttpConstant;
import com.jinke.uang.main.MainActivity;
import com.jinke.uang.model.LoginModule;
import com.jinke.uang.presenter.CommonPresenter;
import com.jinke.uang.rxbus.EventMsg;
import com.jinke.uang.rxbus.Events;
import com.jinke.uang.rxbus.RxBus;
import com.jinke.uang.utils.StringUtils;
import com.jinke.uang.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 老用户登录界面
 */
@SuppressLint("CheckResult")
@Route(name = "老用户登录页", path = RouterMap.LOGIN_PAGE)
public class LoginActivity extends BaseNormalActivity<CommonPresenter, LoginModule> {

    public static final String USER_NAME = "user_name";

    @BindView(R.id.et_userName)
    EditText etUserName;
    @BindView(R.id.et_password)
    EditText etPassword;


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent != null && !StringUtils.isEmpty(intent.getStringExtra(USER_NAME))) {
            etUserName.setText(intent.getStringExtra(USER_NAME));
        }
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void onUIReady() {}

    @Override
    protected int getContentResId() {
        return R.layout.activity_login;
    }

    @Override
    public String getPageTitle() {
        return null;
    }

    @Override
    protected CommonPresenter getPresenter() {
        return new CommonPresenter();
    }

    @Override
    protected LoginModule getModule() {
        return new LoginModule();
    }

    @OnClick({R.id.tv_login, R.id.tv_register, R.id.forget_psw})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                login();
                break;
            case R.id.forget_psw:
                ARouter.getInstance().build(RouterMap.FIND_PASSWORD_PAGE).navigation();
                break;
            case R.id.tv_register:
                ARouter.getInstance().build(RouterMap.REGISTER_PAGE).navigation();
                break;
            default:
                break;
        }
    }

    private void login() {
        String userName = etUserName.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        if (TextUtils.isEmpty(userName)) {
            ToastUtils.showShort(this, R.string.login_type_user_name);
            return;
        } else if (TextUtils.isEmpty(password)) {
            ToastUtils.showShort(this, R.string.login_type_pwd);
            return;
        }
        showLoadingView();
        mPresenter.getData(HttpConstant.OLD_LOGIN_DATA,null, userName, password);
    }

    //返回值
    @Override
    public void showResponse(int apiType,String listType, Object o) {
        if (apiType == HttpConstant.OLD_LOGIN_DATA) {//登录成功
            dismissLoadingView();
            BaseResponse<LoginData> baseLogin = (BaseResponse<LoginData>) o;

            if (baseLogin.getStatus() == Constant.HTTP_OK) {
                String token = baseLogin.getData().getToken();

                if (StringUtils.isEmpty(token)) {
                    ToastUtils.showShort(this, getStringResource(R.string.token_is_null));
                    return;
                }
                UserInfo.setToken(token);
                ToastUtils.showShort(this, getStringResource(R.string.login_page_login_success));

                Acache.get(getApplication()).put("main","1");
                Intent intent=new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                EventMsg msg = new EventMsg();
                msg.setMsg(Events.LOGIN);
                RxBus.getInstance().post(msg);
                finish();
            } else {
                ToastUtils.showShort(this, baseLogin.getMessage());
            }
        }
    }

    @Override
    public void showError(Throwable e) {
        dismissLoadingView();
    }
}



